package com.ht.api.call.func.impl.transfer;

import cn.hutool.json.JSONObject;
import com.ht.api.bean.AjaxResult;
import com.ht.api.bean.LoginBean;
import com.ht.api.call.func.IFunctionCall;
import com.ht.api.db.Db;
import com.ht.api.db.Entity;

import java.util.ArrayList;
import java.util.List;

/**
 * 新增待继承用户
 */
public class AddCustomersToTransferFunctionCall implements IFunctionCall {


    @Override
    public String getName() {
        return "新增待继承用户";
    }

    @Override
    public JSONObject execute(LoginBean me, JSONObject param, AjaxResult result) {

        List<String> externalUserids = param.getBeanList("external_userids", String.class);

        String transferId = param.getStr("transfer_id");
        Entity wxTransferUser = Db.createSqlParser("wx_transfer_user")
                .select("main_id,appid,type,handover_userid,takeover_userid,transfer_time").where("main_id", transferId).findOne(me.getDataSource());

        String takeoverUserid = wxTransferUser.getStr("takeover_userid");
        String transferTime = wxTransferUser.getStr("transfer_time");
        String appid = wxTransferUser.getStr("appid");
        String handoverUserid = wxTransferUser.getStr("handover_userid");

        int type = wxTransferUser.getInt("type");


        List<Entity> dataList = new ArrayList<>();
        for (String externalUserid : externalUserids) {
            Entity wxTransferUserItem = Db.createEntity("wx_transfer_user_item");
            wxTransferUserItem.set("appid", appid);
            wxTransferUserItem.set("transfer_id", transferId);
            wxTransferUserItem.set("handover_userid", handoverUserid);
            wxTransferUserItem.set("takeover_userid", takeoverUserid);
            wxTransferUserItem.set("external_userid", externalUserid);
            wxTransferUserItem.set("status", 0);

            dataList.add(wxTransferUserItem);

            if (type == 1) {
                Entity workuserDimissionExternal = Entity.create("wx_workuser_dimission_external");
                workuserDimissionExternal.set("appid", appid);
                workuserDimissionExternal.set("handover_userid", handoverUserid);
                workuserDimissionExternal.set("external_userid", externalUserid);
                workuserDimissionExternal.set("takeover_userid", takeoverUserid);
                workuserDimissionExternal.set("transfer_time", transferTime);
                workuserDimissionExternal.set("statue", 6);
                dataList.add(workuserDimissionExternal);
            }
        }

        if (type == 1) {
            Entity wxWorkuserDimission = Entity.create("wx_workuser_dimission");
            wxWorkuserDimission.set("appid", appid);
            wxWorkuserDimission.set("handover_userid", handoverUserid);
            wxWorkuserDimission.set("wait_cnt", -externalUserids.size());
            dataList.add(wxWorkuserDimission);
        }

        wxTransferUser.set("all_cnt", externalUserids.size());
        dataList.add(wxTransferUser);

        Db.insertBatchWithStream(me.getStreamLoadConfig(), dataList);

        return null;
    }
}
